function ohelp = aft_Help(hfile, smeth, varargin)
% AFT::Help  - method for any BVQXfile type
%
% FORMAT:       [helptext] = obj.Help([methodname, onerror]);
%
% Input fields:
%
%       methodname  if given, only the help for one method is returned
%       onerror     checks global flag and only returns help if requested
%
% Output fields:
%
%       helptext    1xN char array describing all (or one) method(s)
%
% TYPES: ALL

% Version:  v0.8a
% Build:    9102122
% Date:     Oct-21 2009, 10:13 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global config
global bvqxfile_config;

% only valid for single file
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, true)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
if nargin > 2 && ...
   ~bvqxfile_config.settings.Behavior.HelpOnError
    ohelp = '';
    return;
end

% get object super-struct
sc = bvqxfile_getscont(hfile.L);

% locate methods for this type
ftype = sc.S.Extensions{1};
meth = BVQXfile(0, 'methods');
if ~isfield(meth, lower(ftype))
    ohelp = ['No methods for filetype ' upper(ftype) '.'];
    return;
end

% get methods
ameth = meth.aft;
ometh = meth.(lower(ftype));

% only single method
if nargin > 1 && ...
    ischar(smeth) && ...
   ~isempty(smeth)
    smeth = regexprep(smeth(:)', '\s*\(.*$', '');
    if isfield(ameth, lower(smeth)) || ...
        isfield(ometh, lower(smeth))

        % get single methods help
        if isfield(ometh, lower(smeth))
            mfile = ometh.(lower(smeth));
        else
            mfile = ameth.(lower(smeth));
        end
        mhelp = help(['@BVQXfile/private/' mfile{1} '.m']);
        ohelp = gluetostring(splittocell(mhelp, char([10, 13]), 1, 1), char(10));
        if nargin > 2
            ohelp = [char([10, 10]), ohelp];
        end
    else
        ohelp = ['No ' smeth ' method for filetype ' upper(ftype) '.'];
    end
    return;
end

% combine ameth and ometh
amthf = fieldnames(ameth);
omthf = fieldnames(ometh);
aodis = [zeros(numel(amthf), 1); ones(numel(omthf), 1)];
[tmthf, tns] = sort([amthf; omthf]);
aodis = aodis(tns);

% prepare output
ohelp = cell(numel(tns), 1);

% iterate over methods
fcall = [upper(ftype) '::'];
for mc = numel(tns):-1:1
    
    % for known methods:
    if aodis(mc) == 1
        ohelp{mc} = sprintf('%-74s / Syntax: %s', sprintf('%-27s - %s', ...
            [fcall, regexprep(ometh.(tmthf{mc}){1}, '^\w+_', '')], ...
            ometh.(tmthf{mc}){3}), ometh.(tmthf{mc}){4});
    elseif any(strcmpi(ftype, ameth.(tmthf{mc}){5}))
        ohelp{mc} = sprintf('%-74s / Syntax: %s', sprintf('%-27s - %s', ...
            [fcall, regexprep(ameth.(tmthf{mc}){1}, '^\w+_', '')], ...
            ameth.(tmthf{mc}){3}), strrep(ameth.(tmthf{mc}){4}, 'obj.', ...
            [ftype '.']));
    else
        ohelp(mc) = [];
    end
end

% glue to string
ohelp = gluetostring(ohelp, char(10));
